package com.cleverest.cleverestdiary.service.impl;

import com.cleverest.cleverestdiary.mapper.ArticleCategoryMapper;
import com.cleverest.cleverestdiary.pojo.ArticleCategory;
import com.cleverest.cleverestdiary.service.ArticleCategoryService;
import com.cleverest.cleverestdiary.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public ArticleCategory findByArticleCategoryName(String name) {
        ArticleCategory ac = articleCategoryMapper.findByArticleCategoryName(name);
        return ac;
    }

    @Override
    public void add(ArticleCategory articleCategory) {
        //补充属性值
        articleCategory.setCreateTime(LocalDateTime.now());
        articleCategory.setUpdateTime(LocalDateTime.now());

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        articleCategory.setCreateUser(userId);
        articleCategoryMapper.add(articleCategory);
    }

    @Override
    public List<ArticleCategory> list() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return articleCategoryMapper.list(userId);
    }

    @Override
    public ArticleCategory findById(Integer id) {
        ArticleCategory c = articleCategoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(ArticleCategory articleCategory) {
        articleCategory.setUpdateTime(LocalDateTime.now());
        articleCategoryMapper.update(articleCategory);
    }

    @Override
    public void delete(Integer id) {
        articleCategoryMapper.delete(id);
    }
}
